package com.wx.mall.goods.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wx.mall.common.api.PageVo;
import com.wx.mall.common.exception.BusinessException;
import com.wx.mall.common.exception.code.TemplateResponseCode;
import com.wx.mall.goods.entity.Template;
import com.wx.mall.goods.mapper.ProTemplateMapper;
import com.wx.mall.goods.service.ITemplateService;
import com.wx.mall.goods.vo.req.TemplateUpdateReqVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 模板表 服务实现类
 * </p>
 *
 * @author LiRui
 * @since 2020-03-13
 */
@Service
public class TemplateServiceImpl extends ServiceImpl<ProTemplateMapper, Template> implements ITemplateService {

    @Override
    public Page<Template> pageInfo(PageVo vo) {
        Page<Template> page = new Page<>(vo.getPageNum(), vo.getPageSize());
        Page<Template> templatePage = baseMapper.selectPage(page, null);
        //当前页数据
//        pageVO.setList(templatePage);
//        //总页数
//        pageVO.setTotalRows(page.getTotal());
//        //当前第几页
//        pageVO.setPageNum(page.getCurrent());
//        //每页记录数
//        pageVO.setPageSize(page.getSize());
        return templatePage;
    }

    @Override
    public void deleteById(Integer id) {
        Integer deleteById = baseMapper.deleteById(id);
//        System.out.println(deleteById);
//        if (deleteById == 0) {
//            System.out.println("进入这个方法了");
//            throw new BusinessException(TemplateResponseCode.DELETE_ERROP);
//        }
    }

    @Override
    public void updateById(TemplateUpdateReqVO vo) {
        Template proTemplate = new Template();
        //将vo中的数据复制到实体类中
        BeanUtils.copyProperties(vo, proTemplate);
        Integer updateById = baseMapper.updateById(proTemplate);
        if (updateById == 0) {
            throw new BusinessException(TemplateResponseCode.UPDATE_ERROP);
        }
    }

    @Override
    public void add(String name) {
        QueryWrapper<Template> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        List<Template> templates = baseMapper.selectList(queryWrapper);
        if (templates.size() != 0) {
            throw new BusinessException(TemplateResponseCode.ADD_ERROP);
        }
        Template proTemplate = new Template();
        System.out.println(name);
        proTemplate.setName(name);
        proTemplate.setParaNum(0);
        proTemplate.setSpecNum(0);
        baseMapper.insert(proTemplate);
    }
}
